﻿using _8Group.MES.ILiService;
using _8Group.MES.ILiService.DTO;
using _8Group.MES.MeasuringUnit;
using _8Group.MES.Model.DV设备管理;
using _8Group.MES.WorkStation;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.LiService
{
    [ApiExplorerSettings(GroupName = "李明豫")]
    public class DVMachineryTypeService:ApplicationService,IDVMachineryTypeService
    {
        private readonly IRepository<DV_Machinery_Type,int> _machineryTypeRepository;

        public DVMachineryTypeService(IRepository<DV_Machinery_Type,int> machineryTypeRepository)
        {
            _machineryTypeRepository = machineryTypeRepository;
        }
        /// <summary>
        /// 获取全部设备类型设置
        /// </summary>
        /// <param name="machineTypeName"></param>
        /// <param name="enableFlag"></param>
        /// <returns></returns>
        public async Task<object> GetDVMachineryType(string? machineTypeName, string? enableFlag)
        {
            var dvMachineryTypes = await _machineryTypeRepository.GetQueryableAsync();
            var dvMachineryTypesList = dvMachineryTypes.AsQueryable();
            if (!string.IsNullOrEmpty(machineTypeName))
            {
                dvMachineryTypesList = dvMachineryTypesList.Where(x => x.MachineTypeName.Contains(machineTypeName));
            }
            if (!string.IsNullOrEmpty(enableFlag))
            {
                dvMachineryTypesList = dvMachineryTypesList.Where(x => x.EnableFlag.ToString()==enableFlag);
            }
            var resule = dvMachineryTypesList.Select(m => new DVMachineryTypeListDTO
            {
                ParentTypeId = m.ParentTypeId,
                MachineTypeCode = m.MachineTypeCode,
                MachineTypeName = m.MachineTypeName,
                Ancestors = m.Ancestors,
                EnableFlag = m.EnableFlag,
                Remark = m.Remark,
                Id=m.Id,
                CreateBy = m.CreateBy,
                CreateTime = m.CreateTime,
            }).ToList();
            var tree = GetTreeDVMachineryType(resule);
           
            return new ResultResturn<List<DVMachineryTypeListDTO>> { code = "0", data = tree, count = resule.Count(), msg = "success" };
        }

        /// <summary>
        /// 获取树形结构的设备类型设置
        /// </summary>
        private List<DVMachineryTypeListDTO> GetTreeDVMachineryType(IEnumerable<DVMachineryTypeListDTO> dvItemTypes, int? pid = null)
        {
            var list = dvItemTypes.ToLookup(m => m.ParentTypeId);
            return list[pid ?? 0].Select(m => new DVMachineryTypeListDTO
            {
                ParentTypeId = m.ParentTypeId,
                MachineTypeCode = m.MachineTypeCode,
                MachineTypeName = m.MachineTypeName,
                EnableFlag = m.EnableFlag,
                Remark = m.Remark,
                Id = m.Id,
                children = GetTreeDVMachineryType(dvItemTypes,m.Id)
            }).ToList();
        }

        /// <summary>
        /// 修改设备类型设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<bool> UpdateMachineryType(UpdateMachineryDTO input)
        {
            input.UpdateBy = "李明豫";
            input.UpdateTime = DateTime.Now;
            var machineryType = AutoMapperExt.MapTo<DV_Machinery_Type>(input);
            await _machineryTypeRepository.UpdateAsync(machineryType);
            return true;
        }
    }
   
}
